package com.xkk.controller;

import com.xkk.mapper.EmployeeMapper;
import com.xkk.pojo.Employee;
import com.xkk.service.EmployeeService;
import com.xkk.service.EmployeeServiceImpl;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Collection;
import java.util.List;

@Controller
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @RequestMapping("/emps")
    public String list(Model model) {
        List<Employee> list = employeeService.selectAllEmployee();
        model.addAttribute("list", list);
        //    System.out.println(list);
        return "emp/list";
    }

    //添加员工
    @RequestMapping("/toadd")
    public String toAdd(Model model) {
        Employee employee = new Employee();
        model.addAttribute("add", employee);
        return "emp/add";
    }

    @PostMapping("/add")
    public String toEmp(Employee employee) {
        employeeService.addEmployee(employee);
        return "redirect:/emps";
    }

    //修改员工
    @GetMapping("/emp/{id}")
    public String toUpdateEmp(@PathVariable("id") Integer id, Model model) {
        //查出原来的数据
        Employee employee = employeeService.selectEmployeeById(id);
        model.addAttribute("emp", employee);
        return "emp/update";
    }

    @PostMapping("/update")
    public String updateEmp(Employee employee) {

        employeeService.updateEmployee(employee);
        return "redirect:/emps";
    }

    //删除员工信息
    @GetMapping("/delemp/{id}")
    public String deleteEmp(@PathVariable("id") int id) {
        employeeService.deleteEmployee(id);
        return "redirect:/emps";
    }
//    @GetMapping("/emp")
//    public String toAddpage(Model model){
//        //查出所有部门的信息
//        Collection<Department> departments = departmentDao.getDepartments();
//        model.addAttribute("departments",departments);
//        return "emp/add";
//    }
//    @PostMapping("/emp")
//    public String toEmp(Employee employee){
//        System.out.println("save=>"+employee);
//        employeeDao.save(employee);//调用底层业务的方法保存员工的信息
//
//        //添加的操作 forward
//        return "redirect:/emps";
//    }
//    //去员工的修改页面
//    @GetMapping("/emp/{id}")
//    public String toUpdateEmp(@PathVariable("id")Integer id, Model model){
//        //查出原来的数据
//        Employee employee = employeeDao.getEmployeeById(id);
//        model.addAttribute("emp",employee);
//        //查出所有部门的信息
//        Collection<Department> departments = departmentDao.getDepartments();
//        model.addAttribute("departments",departments);
//        return "emp/update";
//    }
//    @PostMapping("/updateEmp")
//    public String updateEmp(Employee employee){
//        employeeDao.save(employee);
//        return "redirect:/emps";
//    }
//    @GetMapping("/delemp/{id}")
//    public String deleteEmp(@PathVariable("id")int id){
//        employeeDao.delete(id);
//        return "redirect:/emps";
//    }
}
